<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="机器唯一标识">
          <a-input placeholder="请输入机器唯一标识" v-decorator="['chipid', validatorRules.chipid ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="用户唯一标识">
          <a-input placeholder="请输入用户唯一标识" v-decorator="['appUuid', validatorRules.appUuid ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="机器名称">
          <a-input placeholder="请输入机器名称" v-decorator="['productName', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="绑定的萤石云appkey">
          <a-input placeholder="请输入绑定的萤石云appkey" v-decorator="['ysyappkey', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="机器的ip">
          <a-input placeholder="请输入机器的ip" v-decorator="['ip', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="订阅号">
          <a-input placeholder="请输入订阅号" v-decorator="['subscribe', validatorRules.subscribe ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设置的时间">
          <a-input placeholder="请输入设置的时间" v-decorator="['time', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="mac地址">
          <a-input placeholder="请输入mac地址" v-decorator="['macAddress', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否绑定">
          <a-input placeholder="请输入是否绑定" v-decorator="['isBind', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否为分享">
          <a-input placeholder="请输入是否为分享" v-decorator="['isShare', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否超出分享时间">
          <a-input placeholder="请输入是否超出分享时间" v-decorator="['isExpire', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分享到期时间">
          <a-input placeholder="请输入分享到期时间" v-decorator="['expireTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否在线">
          <a-input placeholder="请输入是否在线" v-decorator="['isLine', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="ap的mac地址">
          <a-input placeholder="请输入ap的mac地址" v-decorator="['softAPmacAddress', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="apWiFi名">
          <a-input placeholder="请输入apWiFi名" v-decorator="['wifiname', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="apwifi密码">
          <a-input placeholder="请输入apwifi密码" v-decorator="['wifipassword', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="操作等级">
          <a-input-number v-decorator="[ 'level', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="机器型号">
          <a-input placeholder="请输入机器型号" v-decorator="['espType', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="绑定的萤石云序列号">
          <a-input placeholder="请输入绑定的萤石云序列号" v-decorator="['ysyserialnumber', {}]" />
        </a-form-item>
		
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "espadminModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        chipid:{rules: [{ required: true, message: '请输入机器唯一标识!' }]},
        appUuid:{rules: [{ required: true, message: '请输入用户唯一标识!' }]},
        subscribe:{rules: [{ required: true, message: '请输入订阅号!' }]},
        },
        url: {
          add: "/espadmin/espadmin/add",
          edit: "/espadmin/espadmin/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'chipid','appUuid','productName','ysyappkey','ip','subscribe','time','macAddress','isBind','isShare','isExpire','expireTime','isLine','softAPmacAddress','wifiname','wifipassword','level','espType','ysyserialnumber'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>